package com.citywithincity.db.validate.oracle;

import java.sql.ResultSetMetaData;

import com.citywithincity.db.DbValidateException;
import com.citywithincity.db.interfaces.IColumnDataValidate;
import com.citywithincity.utils.StringUtils;

/**
 * 如果是oracle则需要
 * @author Randy
 *
 */
public class Varchar2Validate implements IColumnDataValidate{
	private int len;
	private boolean isNullable;
	public Varchar2Validate(int len,int isNullable) {
		this.len = len;
		if(isNullable==ResultSetMetaData.columnNullable){
			this.isNullable = true;
		}else{
			this.isNullable = false;
		}
		
	}
	
	@Override
	public Object validateData(Object data) throws DbValidateException {
		if(data==null && !isNullable){
			throw new DbValidateException("不能为空");
		}
		String value = String.valueOf(data);
		if(StringUtils.getRealLength(value) > len){
			throw new DbValidateException(String.format("长度应该小于%d个",len));
		}
		return value;
	}

}
